<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class map_controller extends wxapp_controller{
	function list_action(){
	    $coordinates=$this->Convert_GCJ02_To_BD09($_POST['x'],$_POST['y']);
	    $x=$coordinates['lng'];
	    $y=$coordinates['lat'];
	    //根据sql距离计算公式计算距离并排序
	    $select="`id`,`uid`,`name`,`minsalary`,`maxsalary`,`lastupdate`,6371 * acos(cos(radians(".$y.")) * cos(radians(`y`)) * cos(radians(`x`) - radians(".$x.")) + sin(radians(".$y.")) * sin(radians(`y`))) AS `distance`";
	    $limit=$_POST['limit']?$_POST['limit']:30;
	    $JobM=$this->MODEL('job');
	    $rows=$JobM->GetComjobList(array('r_status'=>'1','status'=>'0','state'=>'1','x>'=>0,'y>'=>0),array('field'=>$select,'limit'=>$limit,'orderby'=>'distance','desc'=>'asc'));
	    if ($rows){
	        foreach ($rows as $v){
	            $uids[]=$v['uid'];
	        }
	        $CompanyM=$this->MODEL('company');
	        $com = $CompanyM->GetComList(array("`uid` in (".pylode(',', $uids).")"),array('field'=>'`uid`,`name`,`shortname`'));
	        foreach ($rows as $k=>$v){
	            $list[$k]['id']      =$v['id'];
	            $list[$k]['name']    =mb_substr($v['name'], 0,16,'utf-8');
	            if ($v['minsalary']){
	                if ($v['maxsalary']>0){
	                    $list[$k]['salary_n']='￥'.$v['minsalary'].'-'.$v['maxsalary'];
	                }else{
	                    $list[$k]['salary_n']='￥'.$v['minsalary'].'以上';
	                }
	            }else{
	                $list[$k]['salary_n']='面议';
	            }
	            if($v['distance']<=1){
	                $list[$k]['dis']=ceil($v['distance']*1000).'m';
	            }else{
	                $list[$k]['dis']=round($v['distance'], 2).'km';
	            }
	            foreach ($com as $val){
	                if ($val['uid']==$v['uid']) {
	                    if($v['shortname']){
	                        $list[$k]['com_name']=mb_substr($val['shortname'], 0,16,'utf-8');
	                    }else{
	                        $list[$k]['com_name']=mb_substr($val['name'], 0,16,'utf-8');
	                    }
	                }
	            }
	        }
	        $data['list']=count($list)>0?$list:array();
	        $data['error']=1;
	    }else{
	        $data['error']=2;
	    }
		echo json_encode($data);die;
	}
    /*
     * 中国正常GCJ02坐标---->百度地图BD09坐标
     * 腾讯地图用的也是GCJ02坐标
     * @param double $lat 纬度
     * @param double $lng 经度
     */
    function Convert_GCJ02_To_BD09($lng,$lat){
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $lng;
        $y = $lat;
        $z =sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
        $lng = $z * cos($theta) + 0.0065;
        $lat = $z * sin($theta) + 0.006;
        return array('lng'=>$lng,'lat'=>$lat);
    }
}
?>